//
//  CollectionViewLayout.m
//  BeautyClother
//
//  Created by dllo on 15/9/19.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "CollectionViewLayout.h"
#import "ItemsModel.h"
@interface CollectionViewLayout ()

@property (nonatomic, assign)CGFloat offLeft;
@property (nonatomic, assign)CGFloat offRight;

@property (nonatomic, assign)CGFloat Width;

@end

@implementation CollectionViewLayout

- (void)dealloc
{
    [_array release];
    [_dicArray release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.array = [NSMutableArray array];
        self.dicArray = [NSMutableArray array];
        
    }
    return self;
}

- (void)prepareLayout
{
    [super prepareLayout];
    [self calculatorHeight];
}

- (CGSize)collectionViewContentSize
{
    CGFloat offwidth = [UIScreen mainScreen].bounds.size.width / 375;
    if (_offRight > _offLeft) {
        return CGSizeMake(315 * offwidth, _offRight);
        
    } else
    {
        return CGSizeMake(315 * offwidth, _offLeft);
        
    }
}

/// 计算图片cell的自适应高度
- (void)calculatorHeight
{
    [self.array removeAllObjects];
    
    CGFloat offheight = [UIScreen mainScreen].bounds.size.height / 667;
    CGFloat offwidth = [UIScreen mainScreen].bounds.size.width / 375;
    _offLeft = self.height + 300 * offheight;
    _offRight = self.height + 300 * offheight;
    NSInteger num = [self.collectionView numberOfItemsInSection:3];
    for (int i = 0; i < num; i++) {
        if (self.dicArray.count == 0) {
            
        }
        else{
            NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:3];
            UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
            
            if ([[self.dicArray objectAtIndex:i]isKindOfClass:[ItemsModel class]]) {
                
                ItemsModel *model = [self.dicArray objectAtIndex:i];
                
                CGFloat he = model.height;
                CGRect rect = [[UIScreen mainScreen] bounds];
                self.Width = (rect.size.width - 20)/ 2;
//                CGFloat height = he * _Width / model.width + 45* offheight;
                
                if (i % 2 == 0) {
                    att.frame = CGRectMake(5, 10*offheight + _offLeft, _Width, 180 * offheight);
                    _offLeft = _offLeft + 180 * offheight + 5*offheight;
                }else
                {
                    att.frame = CGRectMake(190*offwidth, 10*offheight + _offRight, _Width, 180* offheight);
                    _offRight = _offRight + 180 * offheight + 5*offheight;
                }
                
                
            }
            else{
                NSDictionary *dic = [self.dicArray objectAtIndex:i];
                NSNumber *width = [[dic objectForKey:@"component"] objectForKey:@"width"];
                NSNumber *height = [[dic objectForKey:@"component"] objectForKey:@"height"];
                CGRect rect = [[UIScreen mainScreen] bounds];
                self.Width = (rect.size.width - 20)/ 2;
//                CGFloat realHeight = (height.floatValue * _Width / width.floatValue);
                if (i % 2 == 0) {
                    att.frame = CGRectMake(5, 10 + _offLeft , _Width,180 * offheight);
                    _offLeft +=  180 * offheight + 10;
                } else
                {
                    att.frame = CGRectMake(self.Width + 15, 10 + _offRight, _Width , 180 * offheight);
                    _offRight += 180 * offheight + 10;
                }
                [self.array addObject:att];
            }
        }
        
        NSIndexPath *index = [NSIndexPath indexPathForItem:0 inSection:0];
        UICollectionViewLayoutAttributes *attt = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        CGRect rect = [[UIScreen mainScreen ] bounds];
        attt.frame = CGRectMake(0, 0, rect.size.width, self.height);
        [self.array addObject:attt];
        
        
        NSIndexPath *index1 = [NSIndexPath indexPathForItem:0 inSection:1];
        UICollectionViewLayoutAttributes *at = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index1];
        CGRect rect1 = [[UIScreen mainScreen ] bounds];
        at.frame = CGRectMake(0, self.height + 10, rect1.size.width, 180 * offheight);
        [self.array addObject:at];
        
        NSIndexPath *index2 = [NSIndexPath indexPathForItem:0 inSection:2];
        UICollectionViewLayoutAttributes *a = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index2];
        CGRect rect2 = [[UIScreen mainScreen ] bounds];
        a.frame = CGRectMake(0, at.frame.size.height + at.frame.origin.y + 10, rect2.size.width, 100 * offheight);
        [self.array addObject:a];

    }
    
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray *arr = [NSMutableArray array];
    for (UICollectionViewLayoutAttributes *att in self.array) {
        if (CGRectIntersectsRect(att.frame, rect) || CGRectContainsRect(att.frame, rect)) {
            [arr addObject:att];
        }
    }
    return arr;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        return [self.array objectAtIndex:indexPath.item];
    }else
    {
        return [self.array lastObject];
    }
}



@end
